package framework.server;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.apache.log4j.Logger;

import common.LogCategory;

import framework.Admin;

/**
 * The command prompt admin of the framework
 *
 * @author	Orlin Tomov
 * @version	0.2
 */
public class Console {

	private static final Logger log = LogCategory.getCategory(Console.class);

	/**
	 * Executes a command and returns the result
	 *
	 * @param	args	command line arguments to send as command for the framework.
	 */
	public static void main(String[] args) {

		try {
			/** determine config */
			int registryPort = Startup.determineRegistryPort(args);

			Registry registry = LocateRegistry.getRegistry(registryPort);
			Admin admin = (Admin)registry.lookup(Server.REGISTRY_NAME);
			String arg = "";
			if (args.length > 0) {
				arg  = args[0];
			}
			String echo = admin.command(arg);
			System.out.println(echo);
		} catch (Exception exception) {
			System.out.println("Could not shutdown server due to:");
			System.out.println(exception.toString());
			/**/log.error("Could not send command to server due to:"+exception.getMessage());
		}
	}
}
